package com.ellecity06.cwhiskymall.adapter

import android.graphics.Color
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.text.buildSpannedString
import androidx.core.text.inSpans
import androidx.recyclerview.widget.RecyclerView
import com.ellecity06.common.base.BaseAdapter
import com.ellecity06.cwhiskymall.bean.LiveRoomMessageModel
import com.ellecity06.cwhiskymall.databinding.ItemLiveRoomMessageBinding
import com.ellecity06.cwhiskymall.utils.textStyle

/**
 * @author ellecity06
 * @time 2024/11/30 16:33
 * @des
 */
class LiveRoomMessageAdapter(private val  onItemViewAttachedListener:((adapterPosition:Int)->Unit)?=null) : BaseAdapter<LiveRoomMessageModel, ItemLiveRoomMessageBinding>() {
    override fun inflateBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ItemLiveRoomMessageBinding {
        return ItemLiveRoomMessageBinding.inflate(layoutInflater, parent, false)
    }
    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
        super.onViewAttachedToWindow(holder)
        onItemViewAttachedListener?.let {
            it.invoke(holder.adapterPosition)
        }
    }

    override fun convert(binding: ItemLiveRoomMessageBinding, position: Int, item: LiveRoomMessageModel) {
        if (item.nickname == "系统聊天") {
            binding.nameTv.setTextColor(Color.parseColor("#FED768"))
            binding.nameTv.text = "${item.nickname}：${item.content}"
        } else {
            binding.nameTv.setTextColor(Color.parseColor("#E7D07E"))
            binding.nameTv.text  = buildSpannedString {
                append(item.nickname)
                append(" ：")
                inSpans(ForegroundColorSpan(Color.WHITE)) {
                    append(item.content)
                }
            }
        }
    }
}